package com.ruantu.clothing.controller;

import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.GongyingshangService;
import com.ruantu.clothing.util.MyException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2018/6/7 0007.
 */
@Controller
@RequestMapping("/gongyingshang")
public class GongyingshangController {
    private static final Logger logger = LoggerFactory.getLogger(GongyingshangController.class);
    @Autowired
    private GongyingshangService gongyingshangService;


    /**
     * 查询所有供应商
     *
     * @return
     */
    @RequestMapping("/list/combobox")
    @ResponseBody
    public List<Gongyingshang> findAllCombobox(HttpSession session) {
        User user = (User) session.getAttribute("info");
        return gongyingshangService.findAll(user.getJgid());
    }

    /**
     * 查询所有供应商
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findAll(Gongyingshang gongyingshang, PageBean pageBean, HttpSession session) {
        User user = (User) session.getAttribute("info");
        gongyingshang.setJgId(user.getJgid());
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(gongyingshangService.findGongyingshangList(gongyingshang, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }

    /**
     * 导出供应商
     *
     * @return
     */
    @RequestMapping("/downloadexcel")
    @ResponseBody
    public void downExcel(Gongyingshang gongyingshang, ServletOutputStream out, HttpServletResponse response, HttpSession session) {
        User user = (User) session.getAttribute("info");
        gongyingshang.setJgId(user.getJgid());
        try {
            XSSFWorkbook wb = gongyingshangService.downExcel(gongyingshang);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode("供应商导出数据.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (IOException e) {
            throw new RuntimeException("导出excel失败，请检查网路重试");
        }
    }


    /**
     * 保存供应商
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage save(Gongyingshang gongyingshang, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (user != null) {
            gongyingshang.setJgId(user.getJgid());
            gongyingshang.setCreateBy(user.getId());
            gongyingshang.setUpdateBy(user.getId());
            try {
                gongyingshangService.saveGongyingshang(gongyingshang);
                jsonResponseMessage.setCode(200);
            } catch (MyException e) {
                logger.error(e.getMessage(), e);
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg(e.getMessage());
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("内部服务器错误，请联系管理员重试");
            }
        } else {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请刷新重试");
        }
        return jsonResponseMessage;
    }


    /**
     * 查询是否存在供应商
     *
     * @return
     */
    @RequestMapping("/find/gname")
    @ResponseBody
    public JsonResponseMessage findGname(Gongyingshang gongyingshang, HttpSession session) {
        User user = (User) session.getAttribute("info");
        gongyingshang.setJgId(user.getJgid());
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            gongyingshangService.findGname(gongyingshang);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
        }
        return jsonResponseMessage;
    }


    /**
     * 根据id查询修改反显
     *
     * @return
     */
    @RequestMapping("/find/id")
    @ResponseBody
    public JsonResponseMessage findById(Gongyingshang gongyingshang) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jsonResponseMessage.setCode(200);
            jsonResponseMessage.setRows(gongyingshangService.findGongyingshangById(gongyingshang));
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请联系管理员重试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 删除供应商
     *
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public JsonResponseMessage delGongyingshang(Gongyingshang gongyingshang) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            gongyingshangService.delGongyingshang(gongyingshang);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        }
        return jsonResponseMessage;
    }

    /**
     * 根据分类查询供应商
     *
     * @return
     */
    @RequestMapping("/combobox/leixing")
    @ResponseBody
    public List<Gongyingshang> findCombobox(Gongyingshang gongyingshang, HttpSession session) {
        User user = (User) session.getAttribute("info");
        gongyingshang.setJgId(user.getJgid());
        return gongyingshangService.findCombobox(gongyingshang);
    }


}
